package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.api.wemedia.IWemediaClient;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.common.commons.ArticleConstants;
import com.heima.common.commons.CacheService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vo.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmChannel;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class HotArticleServiceImpl implements HotArticleService {
    @Autowired
    ApArticleMapper apArticleMapper;

    /**
     * 计算热点文章
     */
    @Override
    public void computeHotArticle() {
        //查询前五天的文章数据
        Date dateparam = DateTime.now().minusDays(50).toDate();
        List<ApArticle> apArticleList = apArticleMapper.findArticleList5days(dateparam);
        //计算文章的分值
        List <HotArticleVo> hotArticleVoList = computeHotArticle(apArticleList);
        //为每个频道缓存30条最热的数据
        cachehotmeaasgeforeach(hotArticleVoList);
    }

    /**
     * 为每个channel缓存30条热点数据
     * @param hotArticleVoList
     */
    @Autowired
    IWemediaClient iWemediaClient;
    @Autowired
    CacheService cacheService;
    private void cachehotmeaasgeforeach(List<HotArticleVo> hotArticleVoList) {
        ResponseResult channels = iWemediaClient.getChannels();
        if (channels.getCode().equals(200)){
            String channeljson = JSON.toJSONString(channels.getData());
            List<WmChannel> wmChannels = JSON.parseArray(channeljson, WmChannel.class);
            //检索出每个频道的热点文章
            if (wmChannels!=null&& wmChannels.size()>0){
                for (WmChannel wmChannel : wmChannels) {
                   Integer a =  wmChannel.getId();
                   Integer b =0;
                    for (HotArticleVo hotArticleVo : hotArticleVoList) {
                       b = hotArticleVo.getChannelId();
                    }
                    List<HotArticleVo> alonehotvos = hotArticleVoList.
                            stream().
                            filter(x -> x.getChannelId().equals(wmChannel.getId())).collect(Collectors.toList());
                    //给文章排序，取30条最热的放入redis
                    List<HotArticleVo> besthotmessage = alonehotvos.stream().sorted(Comparator.comparing(HotArticleVo::getScore).reversed()).collect(Collectors.toList());
                    if (besthotmessage.size()>30){
                        besthotmessage = besthotmessage.subList(0,30);
                    }
                    cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + wmChannel.getId(),JSON.toJSONString(besthotmessage));
                }
                //给推荐频道存
                List<HotArticleVo> besthotmessage = hotArticleVoList.stream().sorted(Comparator.comparing(HotArticleVo::getScore).reversed()).collect(Collectors.toList());
                if (besthotmessage.size()>30){
                    besthotmessage = besthotmessage.subList(0,30);
                }
                    cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + ArticleConstants.DEFAULT_TAG,JSON.toJSONString(besthotmessage));
            }
        }
    }

    /**
     * 计算文章分值
     * @param apArticleList
     * @return
     */
    private List<HotArticleVo> computeHotArticle(List<ApArticle> apArticleList) {
        ArrayList<HotArticleVo> volist = new ArrayList<>();
        if (apArticleList != null && apArticleList.size()>0){
            for (ApArticle apArticle : apArticleList) {
                HotArticleVo hotArticleVo = new HotArticleVo();
                BeanUtils.copyProperties(apArticle,hotArticleVo);
                 Integer score = computescore(apArticle);
                 hotArticleVo.setScore(score);
                 volist.add(hotArticleVo);
            }
        }
        return volist;
    }

    /**
     * 计算每一个文章分值的具体操作
     * @param apArticle
     * @return
     */
    private Integer computescore(ApArticle apArticle) {
        Integer scere = 0;
        if(apArticle.getLikes() != null){
            scere += apArticle.getLikes() * ArticleConstants.HOT_ARTICLE_LIKE_WEIGHT;
        }
        if(apArticle.getViews() != null){
            scere += apArticle.getViews();
        }
        if(apArticle.getComment() != null){
            scere += apArticle.getComment() * ArticleConstants.HOT_ARTICLE_COMMENT_WEIGHT;
        }
        if(apArticle.getCollection() != null){
            scere += apArticle.getCollection() * ArticleConstants.HOT_ARTICLE_COLLECTION_WEIGHT;
        }
        return scere;
    }
}
